package com.zbl.zblproject.wxpublichao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.wxpublichao.entity.SignUp;
import com.zbl.zblproject.wxpublichao.mapper.SignUpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/8/26
 */
@RestController
@RequestMapping("/signUp")
public class SignUpController {

    @Autowired
    private SignUpMapper signUpMapper;

    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@RequestParam("time") String time,@RequestParam("name") String name
            ,@RequestParam("shopName") String shopName,@RequestParam("phone") String phone){

        //
        QueryWrapper<SignUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        SignUp s=signUpMapper.selectOne(queryWrapper);
        if(s!=null){
            return ResultUtil.error("数据已存在！");
        }else{
            SignUp signUp= new SignUp();
            signUp.setName(name);
            signUp.setShopName(shopName);
            signUp.setTime(time);
            signUp.setPhone(phone);
            signUp.setCreateTime(System.currentTimeMillis()/1000);
            if (signUpMapper.insert(signUp)==1){
                return ResultUtil.success("成功");
            }else{
                return ResultUtil.error("失败");
            }
        }


    }

    @PostMapping("/list")
    @ResponseBody
    public Result list(){
        QueryWrapper<SignUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return ResultUtil.success("成功",signUpMapper.selectList(queryWrapper));
    }
}
